/*
 * 描述： <描述>
 * 修改人： rain
 * 修改时间： 2016年6月25日
 * 项目： rainhy-core
 */
package com.rainhy.core.util;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.rainhy.core.exception.RainhyException;

/**
 * Http 工具
 * 
 * @author rain
 * @version [版本号, 2016年6月25日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
// FIXME 准备重构此方法的所有类
public class HttpUtils {
    
    /**
     * 
     * get 协议请求
     *
     * @param url
     * 
     * @return String [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     * @version [版本号, 2016年3月7日]
     * @author rain
     */
    public static String sendGetMethod(String url) {
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            HttpGet httpGet = new HttpGet(url);
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            throw new RainhyException(e, "http GET error");
        } finally {
            IOUtils.closeQuietly(response);
            IOUtils.closeQuietly(client);
        }
    }
    
    /**
     * 
     * 通过 http get 方法获取 JsonObject 数据<br />
     * 如果调用失败,会抛出异常<br />
     * 如果返回的 json 数据中有"errcode",则会抛出异常
     *
     * @param url
     * 
     * @return JsonObject [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     * @version [版本号, 2015年10月27日]
     * @author rain
     */
    public static JsonObject sendGetMethodAndReturnJsonObjectByWeChat(String url) {
        JsonParser jsonParser = new JsonParser();
        
        String responseContent = sendGetMethod(url);
        JsonObject json = jsonParser.parse(responseContent).getAsJsonObject();
        JsonElement errcode = json.get("errcode");
        if (errcode != null && errcode.getAsInt() != 0) {
            throw new RainhyException("geturl异常   ----   " + url + "  ---   " + json.toString());
        }
        return json;
    }
    
    /**
     * 
     * post 协议请求
     *
     * @param url 请求地址
     * @param post 请求的数据
     * 
     * @return String [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     * @version [版本号, 2016年3月7日]
     * @author rain
     */
    public static String sendPostMethod(String url, String post) {
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(post, "UTF-8"));
            response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            throw new RainhyException(e, "http GET error");
        } finally {
            IOUtils.closeQuietly(response);
            IOUtils.closeQuietly(client);
        }
    }
    
    /**
     * 
     * 通过 http post 方法获取 JsonObject 数据
     *
     * @param url
     * @param post数据
     * 
     * @return JsonObject [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     * @version [版本号, 2015年10月27日]
     * @author rain
     */
    public static JsonObject sendPostMethodAndReturnJsonObjectByWeChat(String url, String post) {
        JsonParser jsonParser = new JsonParser();
        String responseContent = sendPostMethod(url, post);
        JsonObject json = jsonParser.parse(responseContent).getAsJsonObject();
        if (!json.get("errcode").toString().equals("0")) {
            throw new RainhyException("posturl异常   ----   " + url + "  ---   " + json.toString());
        }
        return json;
    }
}
